/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.seari.line17;

import com.seari.bean.BaseClient;
import com.seari.line17.handler.Line17Decoder;
import com.seari.line17.handler.Line17Handler;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.util.concurrent.GenericFutureListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Rainbow
 */
public class Line17Client extends BaseClient
{
    
    protected static Logger logger = LoggerFactory.getLogger(Line17Client.class);
    public Line17Client()
    {
        super();
    }
    public Line17Client(String host,int port)
    {
        this.host = host;
        this.port = port;
    }
    
    @Override
    public void setup() throws InterruptedException
    {
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        Line17Client client = this;
        try {  
            Bootstrap b = new Bootstrap();  
            b.group(workerGroup);  
            b.channel(NioSocketChannel.class);  
            b.option(ChannelOption.SO_KEEPALIVE, true);  
            b.handler(new ChannelInitializer<SocketChannel>() {  
                @Override  
                public void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast("line17Decoder",new Line17Decoder());
                    ch.pipeline().addLast("line17hadnler",new Line17Handler(client));  
                }  
            });  
            // Start the client.  
            ChannelFuture f = b.connect(host, port).sync();  
            channel = f.channel();
//            if(f.isSuccess())
//            {
//                System.out.println("连接成功");
//            }else{
//                System.out.println("连接失败 尝试重连");
//                run();
//            }
            // Wait until the connection is closed.  
            if(f.isSuccess())
            {
                logger.info("连接成功");
            }
            //f.channel().closeFuture().sync();  
            f.channel().closeFuture().addListener(new GenericFutureListener<ChannelFuture>(){
                @Override
                public void operationComplete(ChannelFuture f) throws Exception
                {
                    logger.info("Line17 client shutdown gracefully>>>>>>>>>>>>>>>>>");
                    workerGroup.shutdownGracefully();
                }
            });
        } catch(Exception ex)
        {
            logger.warn(ex.getMessage());
        }
//        finally {
//            workerGroup.shutdownGracefully();
//        }
    }
    
    @Override
    public void reset() throws InterruptedException
    {
        if(null != channel)
        {
            Line17Handler handler = (Line17Handler) channel.pipeline().get("line17hadnler");
            if(null != handler)
            {
                handler.closeJedis();
            }
            //channel.close();  //不进行关闭操作,已在channel future中进行优雅关闭
        }
        setup();
    }    
}
